// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev1.service.impl;

import java.util.Iterator;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Document;
import java.io.IOException;
import com.orange.boot.dev1.entity.NewsDetails;
import java.io.Serializable;
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import com.orange.boot.dev1.service.NewsDetailsService;
import org.springframework.stereotype.Service;
import com.orange.boot.dev1.service.NewsOverviewService;
import com.orange.boot.dev1.entity.NewsOverview;
import com.orange.boot.dev1.mapper.NewsOverviewMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class NewsOverviewServiceImpl extends ServiceImpl<NewsOverviewMapper, NewsOverview> implements NewsOverviewService {
    @Autowired
    private NewsDetailsService newsDetailsService;

    public NewsOverviewServiceImpl() {
    }

    public void timingCraw() throws IOException {
        String url = "https://www.cnhnb.com/xt/search/?%E6%9F%91%E6%A9%98%E5%87%A4%E8%9D%B6";
        Document document = Jsoup.connect(url).get();
        Elements elements = document.selectXpath("//div[contains(@class,\"article-img\")]");
        int index = 1;
        Iterator var5 = elements.iterator();

        while(var5.hasNext()) {
            Element item = (Element)var5.next();
            NewsOverview overview = new NewsOverview();
            Element child = item.firstElementChild();
            String href = item.parent().attr("href");
            String URL = "https://www.cnhnb.com" + href;
            overview.setId((long)index);
            overview.setTitle(child.attr("alt"));
            overview.setImage(child.attr("src"));
            overview.setDetailUrl(URL);
            NewsOverview byId = (NewsOverview)this.getById(index);
            if (byId == null) {
                this.save(overview);
            } else {
                overview.setImage(byId.getImage());
                this.updateById(overview);
            }

            StringBuilder sb = new StringBuilder();

            try {
                Document document1 = Jsoup.connect(URL).get();
                Elements elements1 = document1.selectXpath("//div[contains(@class,\"article-title\")]");
                sb.append(((Element)elements1.get(0)).toString());
                elements1 = document1.selectXpath("//div[contains(@class,\"article-tips\")]");
                sb.append(((Element)elements1.get(0)).toString());
                elements1 = document1.selectXpath("//div[contains(@class,\"article-desc\")]");
                sb.append(((Element)elements1.get(0)).toString());
                elements1 = document1.selectXpath("//div[contains(@class,\"article-detail shrink\")]");
                sb.append(((Element)elements1.get(0)).toString());
                int i = sb.indexOf("的精彩图文、热门评论，您可能对");
                if (i != -1) {
                    sb = new StringBuilder(sb.substring(0, i) + "的信息上面的内容就是全部了,如果你对这些内容感兴趣,可以收藏本网站");
                }

                NewsDetails detailsById = (NewsDetails)this.newsDetailsService.getById(index);
                NewsDetails details = new NewsDetails();
                details.setId((long)index);
                details.setOverviewId(overview.getId());
                details.setText(sb.toString());
                details.setUrl(URL);
                if (detailsById == null) {
                    this.newsDetailsService.save(details);
                } else {
                    this.newsDetailsService.updateById(details);
                }

                ++index;
            } catch (IOException var18) {
                NewsDetails details = (NewsDetails)this.newsDetailsService.getById(index);
                details.setId((long)index);
                if (details == null) {
                    this.newsDetailsService.save(details);
                } else {
                    this.newsDetailsService.updateById(details);
                }

                ++index;
                var18.printStackTrace();
            }
        }

    }
}
